﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DimsMapper.Modules;

namespace DimsMapper
{
    public partial class SettingForm : Form
    {
        public SettingForm()
        {
            InitializeComponent();

            tbxFalloutCfgFile.Text = Settings.FalloutCfgFile();
            tbxFolderData.Text = Settings.SaveDataFolder;

            DataBasesList();
        }

        private void DataBasesList()
        {
            if (Main.DataBases() != null) {
                foreach (var db in Main.DataBases())
                {
                    if (db.IsCustom)
                        checkedListBox1.Items.Add(db.DataBasePath, db.IsEnabled);
                }
            }
        }

        private void btnOpenCfgFile_Click(object sender, EventArgs e)
        {
            if (Main.MapsOpenCount() != 0) {
                MessageBox.Show("Перед изменением пути расположения игры сохраните и закройте все редактируемые вами карты.", "Dims Mapper II");
                return;
            }

            if (openFileDialog.ShowDialog() == DialogResult.OK) {
                tbxFalloutCfgFile.Text = openFileDialog.FileName;

                Main.CloseAllDataBases();
                Main.ResetInitialize();

                Settings.SetFalloutCfgFile(openFileDialog.FileName);
                Main.InitializeDataBases();

                checkedListBox1.Items.Clear();
                DataBasesList();

                if (tbxFolderData.Text == string.Empty) {
                    tbxFolderData.Text = Settings.SaveDataFolder;
                }
            }
        }

        private void button2_Click(System.Object sender, EventArgs e)
        {
            folderBrowserDialog.SelectedPath = (Settings.MainGamePath() != null) ? Settings.MainGamePath() : tbxFolderData.Text;

            if (folderBrowserDialog.ShowDialog() == DialogResult.OK) {
                tbxFolderData.Text = folderBrowserDialog.SelectedPath;
                Settings.SaveDataFolder = folderBrowserDialog.SelectedPath;
            }
        }

        private void rbtn866_CheckedChanged(System.Object sender, EventArgs e)
        {
            Settings.encoding = Encoding.GetEncoding("cp866");
        }

        private void rbtnWinDefault_CheckedChanged(System.Object sender, EventArgs e)
        {
            Settings.encoding = Encoding.Default;
        }

        private void rbtnASCII_CheckedChanged(System.Object sender, EventArgs e)
        {
            Settings.encoding = Encoding.ASCII;
        }
    }
}
